<?php
class model extends kernel 
{
	protected $pdo;//数据库
	protected $table;//数据库表名
	protected $id;//记录ID
	private $input;
	
	protected  $where;
	protected  $field;
	
	public function __construct($table,$id) {
		parent::__construct();	
		$this->table = $table;
		$this->id = $id;
		$this->pdo = $this->dbConnect();//连接数据库
		$this->input = $this->loadLib('input');
		date_default_timezone_set('Asia/Shanghai');
	}
	
	/**
	 * 查询一条记录
	 *
	 * @param unknown_type $cond
	 * @param unknown_type $field
	 */
	public function find($cond,$field=null) {
		$field = $this->getField($field);
		
		if(!empty($cond)) {
			$condition = $this->getCondition($cond);//组合where条件		
			$query = "SELECT $field FROM $this->table WHERE $condition";
			$stmt = $this->pdo->prepare($query);//准备查询语句
			$stmt->execute(array_values($cond));//执行查询
			return $stmt->fetch(PDO::FETCH_ASSOC);
		}
	}
	/**
	 * 获取查询的字段
	 *
	 * @param  $field
	 * @return 
	 */
	protected function getField($field) {
		if(empty($field)) return $this->id;
		if(is_array($field)) return join(', ',$field);
		if(is_string($field)) return $field;
	}
	/**
	 * 获取查询数据库的条件
	 *
	 * @param unknown_type $condition
	 * @param unknown_type $arg
	 */
	private function getCondition($condition,$arg='AND') {
		if(empty($condition)) return false;
		
		if(is_numeric($condition)){
			$condition = $this->pdo->quote($condition);
			return "$this->id = $condition";
		}
		
		if(is_array($condition)) {
			$count = 0;
			foreach ($condition as $key=>$value) {
				if($count !== 0) {
					$cond .= " $arg $key = ?";
				} else {
					$cond = "$key = ?";
				}
				$count++;
			}
			return $cond;
		}
		
		if(is_string($condition)) return $condition;
	}
	
	public function findAll($cond,$field=null,$arg=null){
		$field = $this->getField($field);
		
		if(!empty($cond)) {
			$condition = $this->getCondition($cond);
			$query = "SELECT $field FROM $this->table WHERE $condition";
			$stmt = $this->pdo->prepare($query);
			//echo $query;
			if(is_array($cond)){
				//准备查询语句
				$stmt->execute(array_values($cond));//执行查询		
			}
			if(is_string($cond)) {
				$stmt->execute();
			}
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
	
	public function delete($cond) {
		$condition = $this->getCondition($cond);
		$query = "DELETE FROM $this->table WHERE $condition";
		$stmt = $this->pdo->prepare($query);
		$stmt->execute();
		return $stmt->rowCount();	
	}
	
	public function update($cond,$data) {
		$condition = $this->getCondition($cond);
		$update = $this->getUpdateValue($data);
		echo $query = "UPDATE $this->table SET $update WHERE $condition";
		$stmt = $this->pdo->prepare($query);//准备查询语句
		$stmt->execute(array_values($cond));//执行查询
		return $stmt->rowCount();
	}
	
	public function insert($cond,$data) {
		$condition = $this->getCondition($cond);
		$insert = $this->getUpdateValue($data);
		$query = "INSERT INTO $this->table SET $insert WHERE $condition";	
	}
	
	public function getUpdateValue($data) {
		if(is_array($data)) {
			$length = count($data);
			$i = 1;
			foreach ($data as $key=>$value) {
				$value = $this->pdo->quote($value);
				$update .= "$key = $value"; 
				echo $key;
				if($i < $length) $update .= ',';
				$i++;
			}
		
			return $update;
		}
		
		if (is_string($data)) return $data;
		
	}
	
	public function execute($query,$type='') {
		if(!$type) {
			$stmt = $this->pdo->prepare($query);
			$stmt->execute();
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
			
		}
		if($type == 'insert'){
			$this->pdo->exec($query);
			return $this->pdo->lastInsertId(); 
		}

		if($type == 'update') {
			$result = $this->pdo->exec($query);
			return $result;
		}
	}
	
}
?>
